/* 
 * Lop luu thong tin toa do doi tuong Vector
 */
viettel.MVCArray = function(array) {
    viettel.MVCObject.call(this);
    
    this.CLASSNAME = "viettel.MVCArray";
    
    var that = this;
    var mObjects = new Array();

    // Tra ve so phan tu trong mang
    this.getLength = function() {
	return mObjects.length;
    }
    
    // Lay phan tu o cuoi mang va xoa phan tu nay khoi mang
    this.pop = function() {
	return mObjects.pop();
    }
    
    // Them mot phan tu vao cuoi mang
    this.push = function(element) {
	mObjects.push(element);
    }
    
    // Them mot phan tu vao vi tri cu the
    this.insertAt = function(index, element) {
	mObjects.splice(index, 0, element);
    }
    
    // Loai bo phan tu tai vi tri xac dinh
    this.removeAt = function(index) {
	if (index>=0 && index<mObjects.length) {
	    mObjects.splice(index, 1);
	}
    }
    
    // Thiet lap phan tu tai mot vi tri xac dinh
    this.setAt = function(index, element) {
	if (index>=0 && index<mObjects.length) {
	    mObjects[index] = element;
	}
	else {
	    this.insertAt(index, element);
	}
    }
    this.getAt = function(index) {
	if (index>=0 && index<mObjects.length) {
	    return mObjects[index];
	}
	return null;
    }
    
    // Xoa cac phan tu khoi mang
    this.clear = function() {
	mObjects.clear();
    }
    
    // Ham su dung de duyet cac phan tu
    this.forEach = function(callback) {
	for (var i=0; i<mObjects.length; i++) {
	    callback(mObjects[i], i);
	}
    }
    
    // Thiet lap doi tuong dang mang
    this.setArray = function(array) {
	mObjects = new Array();
	if (array!=null && VTObjChecker.isArray(array)) {
	    for (var i=0; i<array.length; i++) {
		mObjects.push(array[i]);
	    }
	}
    }
    this.getArray = function() {
	return mObjects;
    }
    
    // Ham sao chep doi tuong
    // Dam bao cac phan tu trong no phai co ham clone, neu khong chay se khong chuan
    this.clone = function() {
	var objs = new viettel.MVCArray();
	for (var i=0; i<mObjects.length; i++) {
	    var obj = mObjects[i];
	    if (obj!=null && obj.clone!=null) {
		objs.push(obj.clone());
	    }
	    else {
		objs.push(obj);
	    }
	}
	return objs;
    }
    
    // Ham su dung noi bo de tra ve doi tuong LinearRing trong OpenLayers
    // Trong hop nay bat buoc phai la mang cac LatLng
    this._getLinearRing = function() {
	if (mObjects.length>0 && !VTObjChecker.isLatLngObj(mObjects[0])) {
	    VTUtil.throwVTError("This object is not MVCArray<LatLng>"); 
	}
	var linearRing =  new OpenLayers.Geometry.LinearRing();
	for (var i=0; i<mObjects.length; i++) {
	    linearRing.addPoint(viettel.GeometryConverter.convertLatLngToOLPoint(mObjects[i]))
	}
	return linearRing;
    }
	
    // Ham su dung noi bo de tra ve doi tuong LineString trong OpenLayers
    // Trong hop nay bat buoc phai la mang cac LatLng
    this._getLineString = function(lineString) {
	if (lineString==null) {
	    lineString =  new OpenLayers.Geometry.LineString();
	}
	for (var i=0; i<mObjects.length; i++) {
	    lineString.addPoint(viettel.GeometryConverter.convertLatLngToOLPoint(mObjects[i]))
	}
	return lineString;
    }
    
    var init = function(array) {
	that.setArray(array);
    }
    init(array);
}

viettel.MVCArray.prototype = new viettel.MVCObject();
viettel.MVCArray.prototype.constructor = viettel.MVCArray;
var originalProjectionName = "EPSG:4326";
viettel.GeometryConverter = {
	
    convertRawLatLngToOLPoint:function(lat, lng) {
        if (lat==null || lng==null) return null;
        
        var olPoint = new OpenLayers.Geometry.Point(lng, lat);
        if (originalProjectionName!=="EPSG:4326") {
            olPoint = olPoint.transform(userProjection, mapProjection);
        }
        return olPoint;
    },
    
    convertLatLngToOLPoint: function(latLng) {
        if (latLng==null) return null;
        
        var olPoint = new OpenLayers.Geometry.Point(latLng.lng(), latLng.lat());
        if (originalProjectionName!=="EPSG:4326") {
            olPoint = olPoint.transform(userProjection, mapProjection);
        }
        return olPoint;
    },
    
    convertOLPointToLatLng: function(olPoint) {
        if (olPoint==null) return null;
        
        var pt = new OpenLayers.Geometry.Point(olPoint.x, olPoint.y);
        if (originalProjectionName!=="EPSG:4326") {
            pt = pt.transform(mapProjection, userProjection);
        }
        return (new viettel.LatLng(pt.y, pt.x));
    },
    
    convertRawLatLngToOLLonLat: function(lat, lng) {
        if (lat==null || lng==null) return null;
        
        var olLonLat = new OpenLayers.LonLat(lng, lat);
        if (originalProjectionName!=="EPSG:4326") {
            olLonLat = olLonLat.transform(userProjection, mapProjection);
        }
        return olLonLat;
    },
    
    convertLatLngToOLLonLat: function(latLng) {
        if (latLng==null) return null;
        
        var olLonLat = new OpenLayers.LonLat(latLng.lng(), latLng.lat());
        if (originalProjectionName!=="EPSG:4326") {
            olLonLat = olLonLat.transform(userProjection, mapProjection);
        }
        return olLonLat;
    },
    
    convertOLLonLatToLatLng: function(olLonLat) {
        if (olLonLat==null) return null;
        var ll = olLonLat.clone();
        if (originalProjectionName!=="EPSG:4326") {
            ll = ll.transform(mapProjection, userProjection);
        }
        return (new viettel.LatLng(ll.lat, ll.lon));
    },
    
    convertLatLngBoundToOLBound: function(boundary) {
        if (boundary==null) return null;
        
        var b = new OpenLayers.Bounds();
        var sw = boundary.getSouthWest();
        var ne = boundary.getNorthEast();
        b.extend(new OpenLayers.LonLat(sw.lng(), sw.lat()));
        b.extend(new OpenLayers.LonLat(ne.lng(), ne.lat()));
        if (originalProjectionName!=="EPSG:4326") {
            b = b.transform(userProjection, mapProjection);
        }
        return b;
    },
    
    convertOLBoundToLatLngBound: function(boundary) {
        if (boundary==null) return null;
        var b = boundary.clone();
        if (originalProjectionName!=="EPSG:4326") {
            b = b.transform(mapProjection, userProjection);
        }
        var sw = new viettel.LatLng(b.bottom, b.left);
        var ne = new viettel.LatLng(b.top, b.right);
        return new viettel.LatLngBounds(sw, ne);
    }
}